package jmu.edu.yjc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jmu.edu.yjc.constants.SystemConstants;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.MailInfo;
import jmu.edu.yjc.domain.vo.PageVo;
import jmu.edu.yjc.mapper.MailInfoMapper;
import jmu.edu.yjc.service.MailInfoService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 信息表(MailInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-04-06 22:36:50
 */
@Service("mailInfoService")
public class MailInfoServiceImpl extends ServiceImpl<MailInfoMapper, MailInfo> implements MailInfoService {

    @Override
    public ResponseResult updateReadStatus(Long hid) {
        LambdaUpdateWrapper<MailInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(MailInfo::getReadStatus, SystemConstants.READ_STATUS_READ);
        wrapper.eq(MailInfo::getHid,hid);
        update(wrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listMailInfo(Long id) {
        List<MailInfo> mailInfos = baseMapper.get7DayData(id);
        Long total = 0L;
        for (MailInfo info : mailInfos) {
            if(!SystemConstants.READ_STATUS_READ.equals(info.getReadStatus())) {
                total++;
            }
        }
        return ResponseResult.okResult(new PageVo(mailInfos,total));
    }
}
